package com.tarena.lbs.business.servcie.ipml;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.business.dao.repository.BusinessRepository;
import com.tarena.lbs.business.pojo.param.BusinessParam;
import com.tarena.lbs.business.pojo.po.BusinessPO;
import com.tarena.lbs.business.pojo.query.BusinessQuery;
import com.tarena.lbs.business.pojo.vo.BusinessStoreVO;
import com.tarena.lbs.business.pojo.vo.BusinessVO;
import com.tarena.lbs.business.servcie.BusinessService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class BusinessServiceImpl implements BusinessService {
    @Autowired
    private BusinessRepository businessRepository;

    @Override
    public PageResult<BusinessVO> businessList(BusinessQuery query) {
        log.info("查询商家列表{}",query);
        PageResult<BusinessVO> result=new PageResult<>(query.getPageSize(),query.getPageNo());
        PageInfo<BusinessPO> pageInfo=getPageBusiness(query);
        result.setTotal(pageInfo.getTotal());
        List<BusinessPO> pos = pageInfo.getList();
        List<BusinessVO> vos=null;
        if (pos!=null&& pos.size()>0){
            vos=pos.stream().map(po->{
                BusinessVO vo=new BusinessVO();
                BeanUtils.copyProperties(po,vo);
                return vo;
            }).collect(Collectors.toList());
        }
        result.setObjects(vos);
        return result;
    }
    private PageInfo<BusinessPO> getPageBusiness(BusinessQuery query){
        PageHelper.startPage(query.getPageNo(), query.getPageSize());
        List<BusinessPO> pos = businessRepository.list();
        return new PageInfo<>(pos);
    }

    @Override
    public void businessSave(BusinessParam param) {
        log.info("新增商家{}",param);
        BusinessPO  businessPO = new BusinessPO();
        BeanUtils.copyProperties(param,businessPO);
        businessPO.setEntryTime(new Date());
        businessPO.setBusinessIntroduction(param.getBusinessIntroduction());
        businessRepository.businessAdd(param);
    }

    @Override
    public BusinessStoreVO businessStoreDetail(Integer businessId) {
        log.info("查询商家详情{}",businessId);
        BusinessPO po=businessRepository.getById(businessId);
        if(po!=null){
            BusinessStoreVO vo=new BusinessStoreVO();
            BeanUtils.copyProperties(po,vo);
            return vo;
        }
        return null;
    }
}
